unit DaemonLiveUnit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, AppEvnts, IdTCPClient, ConstUnit, DB, ADODB;

type
  TLiveThread = class(TThread)
  private
//    FTcpCli: TIdTCPClient;
    FHost: string;
    FPort: Integer;
  protected
    procedure Execute; override;
  public
    constructor Create(AHost: string; APort: Integer = TcpFilePort);
    destructor Destroy; override;
  end; 

  TfrmDaemonLive = class(TForm)
    AE: TApplicationEvents;
    Database: TADOConnection;
    procedure FormCreate(Sender: TObject);
    procedure AEException(Sender: TObject; E: Exception);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmDaemonLive: TfrmDaemonLive;

implementation

uses FunctionUnit;

{$R *.dfm}

function CheckTcpServer(AHost: string; APort: Integer; AWait: Integer = 200): Boolean;
var
  cli: TIdTCPClient;
  i: Integer;
begin
  Result := False;
  cli := TIdTCPClient.Create(nil);
  try
    cli.Host := AHost;
    cli.Port := APort;

    i := 0;
    repeat
      try
        cli.Connect(AWait);
        Application.ProcessMessages;
        if cli.Connected then
        begin
          cli.WriteInteger(0);
          Result := True;
          Break;
        end;
      except

      end;
      Inc(i);
      Sleep(500);
    until i > 5;

  finally
    cli.Disconnect;
    cli.Free;
  end;
  
end;

procedure TfrmDaemonLive.FormCreate(Sender: TObject);
var
  Ip: string;
begin
  Self.Top := -1000;
  Self.Left := -1000;
  Self.Width := 0;
  Self.Height := 0;
//  ShowWindow(Handle, SW_Hide);
  SetWindowLong(Application.Handle,GWL_EXSTYLE,WS_EX_TOOLWINDOW);

  Database.ConnectionString := Format(DBFormat, [ExtractFilePath(ParamStr(0)) + 'Data\BarData.mdb']);
  Ip := GetSysParams(ServerIps, Database);

  if Ip <> '' then
    TLiveThread.Create(Ip); 
end;

{ TLiveThread }

constructor TLiveThread.Create(AHost: string; APort: Integer = TcpFilePort);
begin
//  FTcpCli := TIdTCPClient.Create(nil);
//  FTcpCli.Host := AHost;
//  FTcpCli.Port := APort;
  FHost := AHost;
  FPort := APort;
  inherited Create(False);
end;

destructor TLiveThread.Destroy;
begin
//  FTcpCli.Free;
end;

procedure TLiveThread.Execute;
var
  i: Integer;
begin
  inherited;
  i := 0;
  while True do
  begin
//    if FTcpCli.Connected then
//      FTcpCli.Disconnect;
//    try
//      FTcpCli.Connect(1000);
//    except
//      on E: Exception do
//      begin
//        LogMessage(Format('LiveThread: %s, %s', [E.ClassName, E.Message]));
//        FindAndKillProcess('WtDaemonService.exe');
//        if not RestartService('DaemonService') then
//        begin
//          FindAndKillProcess('WtDaemonService.exe');
//          RestartService('DaemonService');
//        end;
//        LogMessage('Restart Service: DaemonService');
//      end;
//    end;
//    if FTcpCli.Connected then
//      FTcpCli.Disconnect;

    // 2009-9-12 22:55:38
    if not CheckTcpServer(FHost, FPort, 1000) then
    begin
      FindAndKillProcess('WtDaemonService.exe');
      if not RestartService('DaemonService') then
      begin
        FindAndKillProcess('WtDaemonService.exe');
        RestartService('DaemonService');
      end;
    end;

    Sleep(5000);
  end;
end;

procedure TfrmDaemonLive.AEException(Sender: TObject; E: Exception);
begin
  LogMessage(Format('Exception: %s, Message: %s', [E.ClassName, E.Message]));
end;

end.
